/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 29.07.2005
 *
 *************************************************/
package fightplan.persistence;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;

import fightplan.business.Dojo;
import fightplan.business.Fighter;
import fightplan.business.Gender;
import fightplan.business.Sheet;
import fightplan.business.Tournament;
import fightplan.core.Util;

public class FilePersistence {

	public static boolean save(File file, Tournament tournament, boolean writeFighters) {
		try {
			FileOutputStream fos = new FileOutputStream(file);
			DeflaterOutputStream dos = new DeflaterOutputStream(fos);
			ObjectOutputStream oos = new ObjectOutputStream(dos);
			writeFighters(oos, tournament.getFighters());
			oos.writeObject(tournament);
			oos.close();
			dos.close();
			fos.close();
			return true;
		} catch (IOException iox) {
			iox.printStackTrace();
			return false;
		}
	}

	public static boolean save(File file, Sheet sheet) {
		try {
			FileOutputStream fos = new FileOutputStream(file);
			DeflaterOutputStream dos = new DeflaterOutputStream(fos);
			ObjectOutputStream oos = new ObjectOutputStream(dos);
			oos.writeObject(sheet);
			oos.close();
			dos.close();
			fos.close();
			return true;
		} catch (IOException iox) {
			iox.printStackTrace();
			return false;
		}
	}

	public static Sheet loadSheet(File file) throws Exception {
		FileInputStream fis = new FileInputStream(file);
		InflaterInputStream iis = new InflaterInputStream(fis);
		ObjectInputStream ios = new ObjectInputStream(iis);
		Sheet sheet = (Sheet) ios.readObject();
		ios.close();
		iis.close();
		fis.close();
		sheet.file = file;
		return sheet;
	}

	public static Tournament load(File file, boolean skipFighters) throws Exception {
		FileInputStream fis = new FileInputStream(file);
		InflaterInputStream iis = new InflaterInputStream(fis);
		ObjectInputStream ios = new ObjectInputStream(iis);
		readFighters(ios, new Tournament()); // dummy tournmanet to skip data
		Tournament tournament = (Tournament) ios.readObject();
		ios.close();
		iis.close();
		fis.close();
		return tournament;
	}

	private static void writeFighters(ObjectOutputStream oos, List<Fighter> fighters) throws IOException {
		oos.writeDouble(0.9); // Version
		oos.writeInt(fighters.size());
		for (Fighter fighter : fighters) {
			oos.writeObject(fighter.getName());
			oos.writeInt(fighter.getWeight());
			oos.writeInt(fighter.getYear());
			oos.writeObject(fighter.getGender() != null ? fighter.getGender().getName() : Gender.UNSET.getName());
			oos.writeObject(fighter.getDojo() != null ? fighter.getDojo().getName() : null);
			oos.writeInt(fighter.getBelt());
		}
	}

	public static List<Fighter> readFighters(ObjectInputStream ios, Tournament tournament) throws IOException,
			ClassNotFoundException {
		ios.readDouble(); // = version
		int size = ios.readInt();
		List<Fighter> fighters = new ArrayList<Fighter>(size);
		for (int i = 0; i < size; i++) {
			Fighter fighter = new Fighter();
			fighter.setName((String) ios.readObject());
			fighter.setWeight(ios.readInt());
			fighter.setYear(ios.readInt());

			String gender = (String) ios.readObject();
			fighter.setGender(Gender.parse(gender));

			String dojoName = (String) ios.readObject();
			for (Dojo dojo : tournament.getDojos()) {
				if (Util.equals(dojo.getName(), dojoName)) {
					fighter.setDojo(dojo);
				}
			}
			if (fighter.getDojo() == null) {
				Dojo dojo = new Dojo();
				dojo.setName(dojoName);
				tournament.getDojos().add(dojo);
				fighter.setDojo(dojo);
			}

			fighter.setBelt(ios.readInt());

			fighters.add(fighter);
		}
		return fighters;
	}

}